<template>
  <div class="manege">
    <el-dialog
      :title="modalType === 0 ? '新增流' : '编辑流'"
      :before-close="handleClose"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <!-- 流的表单信息 -->
      <el-form :rules="rules" ref="form" :model="form" label-width="80px">
        <el-form-item label="流名称" prop="name">
          <el-input placeholder="请输入流名称" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="流地址" prop="url">
          <el-input placeholder="请输入流地址" v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input placeholder="请输入备注" v-model="form.remarks"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button @click="handleAdd()" type="primary"> 新增流 </el-button>
      <!-- form搜索区域 -->
      <el-form class="find" :inline="true" :model="urlForm">
        <el-form-item>
          <el-input
            placeholder="请输入流名称"
            v-model="urlForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit(urlForm.name)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <el-table stripe :data="tableData">
        <el-table-column
          prop="name"
          label="流名称"
          width="380"
        ></el-table-column>
        <el-table-column
          prop="url"
          label="流地址"
          width="380"
        ></el-table-column>
        <el-table-column
          prop="remarks"
          label="备注"
          width="380"
        ></el-table-column>
        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pager">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          @current-change="handlePage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { getUrl, addUrl, editUrl, delUrl } from "../src/api";
export default {
  name: "Manage",
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        url: "",
        remarks: "",
      },
      rules: {
        name: [{ required: true, message: "请输入流名称" }],
        url: [{ required: true, message: "请输入流地址" }],
      },
      tableData: [],
      modalType: 0, //0表示新增的弹窗，1表示编辑
      total: 0, //当前的总条数
      pageData: {
        page: 1,
        limit: 10,
      },
      urlForm: {
        name: "",
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    //提交流地址
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //后续对表单的处理  新增流
          if (this.modalType === 0) {
            addUrl(this.form).then((res) => {
              if (res.data.error_code === 0) {
                //重新获取列表的接口
                this.getList();
              }
            });
          } else {
            //编辑流
            editUrl(this.form).then((res) => {
              if (res.data.error_code === 0) {
                //重新获取列表的接口
                this.getList();
              }
            });
          }

          //清空表单的数据
          this.$refs.form.resetFields(); //先编辑再点新增 就会重置表单失败
          this.form.remarks = "";

          //关闭弹窗
          this.dialogVisible = false;
        }
      });
    },
    //弹窗关闭前
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
      this.form.remarks = "";
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.modalType = 1;
      this.dialogVisible = true;
      //注意需要对当前数据进行深拷贝，否则会出错
      this.form = JSON.parse(JSON.stringify(row));
    },
    handleDelete(row) {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUrl( {id: row.id} ).then((res) => {
            if (res.data.error_code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              //重新获取列表的接口
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    //获取列表的数据
    getList(name = "") {
      name ? (this.pageData.page = 1) : "";
      getUrl({ params: { ...this.urlForm, ...this.pageData } }).then((res) => {
        let responseData = res.data;
        if (responseData.error_code == 0) {
          this.tableData = responseData.data.stream_list;
          this.total = responseData.data.count || 0;
        }
      });
    },
    //选择页码的回调函数
    handlePage(val) {
      this.pageData.page = val;
      this.getList();
    },
    //列表的查询
    onSubmit(name) {
      this.getList(name);
    },
  },
};
</script>
<style lang="less" scope>
.manege {
  .manage-header {
    // margin-top: 15px;
    // margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .find {
      margin-top: 22px;
    }
  }
  height: 95%;
  .common-table {
    // position: relative;
    // height: calc(100% - 300px);
    .pager {
      // position: absolute;
      // top: calc(100% - 72px);
      // right: 20px;
      margin-top: 20px;
      float: right;
    }
  }
}
</style>